package com.xxx.video.channel.fragments;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.xxx.video.R;
import com.xxx.video.channel.activitys.AllChannelActivity;
import com.xxx.video.channel.activitys.ChannelListActivity;
import com.xxx.video.channel.adapters.ChannelFragmentTopAdapter;
import com.xxx.video.channel.adapters.HistoryChannelAdapter;
import com.xxx.video.channel.adapters.HotChannelAdapter;
import com.xxx.video.channel.adapters.OtherChannelListAdapter;
import com.xxx.video.channel.beans.ChanelHomeListBean;
import com.xxx.video.channel.config.ChannelFragmentItemClickType;
import com.xxx.video.channel.presenters.ChannelPresenter;
import com.xxx.video.channel.presenters.IChannelView;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.fragments.BaseFragment;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.views.recyclerviews.RefreshRecyclerView;
import com.xxx.video.main.activitys.SearchActivity;
import com.xxx.video.main.activitys.VideoDetailsActivity;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;

public class ChannelFragment extends BaseFragment<ChannelPresenter> implements IChannelView, RefreshRecyclerView.OnRecyclerViewRefreshOrLoadMoreListener {
    @BindView(R.id.iv_actionbar_left_icon)
    ImageView ivActionbarLeftIcon;
    @BindView(R.id.tv_actionbar_left_tile)
    TextView tvActionbarLeftTile;
    @BindView(R.id.v_actionbar_bottom_line)
    View vActionbarBottomLine;
    @BindView(R.id.cl_actionbar)
    ConstraintLayout clActionbar;
    @BindView(R.id.rcv_channel_fragment_list)
    RefreshRecyclerView rcvChannelFragmentList;
    /**
     * adapter
     */
    private ConcatAdapter mConcatAdapter;
    private HotChannelAdapter mHotChannelAdapter;
    private OtherChannelListAdapter mOtherChannelListAdapter;

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        clActionbar.setVisibility(View.VISIBLE);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) clActionbar.getLayoutParams();
        layoutParams.setMargins(0, StatusBarUtil.getInstance().getStatusBarHeight(Objects.requireNonNull(getActivity())), 0, 0);
        clActionbar.setLayoutParams(layoutParams);
        ivActionbarLeftIcon.setVisibility(View.GONE);
        tvActionbarLeftTile.setText(StringUtils.getInstance().getString(R.string.title_channel_fragment));
        vActionbarBottomLine.setVisibility(View.VISIBLE);
    }

    @Override
    public void refreshData(Bundle extras) {
        getPresenter().getChannelInfo();
    }

    @Override
    protected ChannelPresenter createPresenter() {
        return new ChannelPresenter(Objects.requireNonNull(getActivity()), this);
    }

    public static ChannelFragment newInstance(Bundle args) {
        ChannelFragment fragment = new ChannelFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_channel;
    }

    @Override
    public void initData(Bundle extras) {
        initList();
        getPresenter().getChannelInfo();
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void startAllChannelActivity() {
        intent2Activity(AllChannelActivity.class);
    }

    @Override
    public void startChannelListActivity(String fParentId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", fParentId);
        intent2Activity(ChannelListActivity.class, bundle);
    }

    @Override
    public void startVideoDetailsActivity(String id) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", id);
        intent2Activity(VideoDetailsActivity.class, bundle);
    }

    @Override
    public void startSearchActivity() {
        intent2Activity(SearchActivity.class);
    }

    @Override
    public void initList() {
        rcvChannelFragmentList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mConcatAdapter = new ConcatAdapter();
        rcvChannelFragmentList.setAdapter(mConcatAdapter);
        rcvChannelFragmentList.setOnRecyclerViewRefreshOrLoadMoreListener(this);
        rcvChannelFragmentList.setLoadMoreEnabled(false);
        initChannelFragmentTop();
    }

    @Override
    public void initChannelFragmentTop() {
        ChannelFragmentTopAdapter channelFragmentTopAdapter = new ChannelFragmentTopAdapter(Objects.requireNonNull(getActivity()));
        channelFragmentTopAdapter.setOnItemClickListener((o, position, type) -> setItemClick("", position, type));
        mConcatAdapter.addAdapter(channelFragmentTopAdapter);
    }

    @Override
    public void initHistoryChannelList() {
        HistoryChannelAdapter historyChannelAdapter = new HistoryChannelAdapter(Objects.requireNonNull(getActivity()));
        mConcatAdapter.addAdapter(historyChannelAdapter);
    }

    @Override
    public void initHotChannelList(List<ChanelHomeListBean.HotListBean> hotList) {
        if (mHotChannelAdapter == null) {
            mHotChannelAdapter = new HotChannelAdapter(Objects.requireNonNull(getActivity()));
            mConcatAdapter.addAdapter(mHotChannelAdapter);
            mHotChannelAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<ChanelHomeListBean.HotListBean>) (hotListBean, position, type) -> setItemClick(hotListBean.fId, position, type));
        }
        mHotChannelAdapter.setListData(hotList);
    }

    @Override
    public void initOtherChannelList(List<ChanelHomeListBean.TypeListBean> typeList) {
        if (mOtherChannelListAdapter == null) {
            mOtherChannelListAdapter = new OtherChannelListAdapter(Objects.requireNonNull(getActivity()));
            mOtherChannelListAdapter.setOnItemClickListener((BaseRecycleViewAdapter.OnItemClickListener<ChanelHomeListBean.TypeListBean>) (typeListBean, position, type) -> setItemClick(typeListBean.fId, position, type));
            mConcatAdapter.addAdapter(mOtherChannelListAdapter);
        }
        mOtherChannelListAdapter.setData(typeList);
    }

    @Override
    public void setItemClick(String id, int position, int type) {
        switch (type) {
            case ChannelFragmentItemClickType
                    .CHANNEL_FRAGMENT_ITEM_CLICK_TYPE_START_ALL_CHANNEL:
                startAllChannelActivity();
                break;
            case ChannelFragmentItemClickType
                    .CHANNEL_FRAGMENT_ITEM_CLICK_TYPE_START_CHANNEL_LIST:
                startChannelListActivity(id);
                break;
            case ChannelFragmentItemClickType
                    .CHANNEL_FRAGMENT_ITEM_CLICK_TYPE_START_SEARCH:
                startSearchActivity();
                break;
            case ChannelFragmentItemClickType
                    .CHANNEL_FRAGMENT_ITEM_CLICK_TYPE_VIDEO_DETAILS:
                startVideoDetailsActivity(id);
                break;
            default:
                break;
        }
    }

    @Override
    public void initData2View(ChanelHomeListBean successOb) {
        showLayoutStatus(1);
        rcvChannelFragmentList.resetState();
        /*
         * hot list
         */
        if (!ListUtils.isEmpty(successOb.hotList)) {
            initHotChannelList(successOb.hotList);
        }
        /*
         * init vertical channel list
         */
        if (!ListUtils.isEmpty(successOb.typeList)) {
            initOtherChannelList(successOb.typeList);
        }
    }

    @Override
    public void onRecyclerviewRefresh() {
        getPresenter().getChannelInfo();
    }

    @Override
    public void onRecyclerviewLoadMore() {

    }
}
